import { request } from '../../request/request.js'
import { setStorageSync, getStorageSync } from '../../utils/localStorage'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        good: {},
        isCollect: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
    },

    //获取商品详情
    getGoodDetail(goods_id) {
        request({ url: "goods/detail", data: { goods_id } }).then(res => {
            this.setData({
                good: res.data.message,
            })
            let goodCollects = wx.getStorageSync("goodCollects") || [];
            let isCollect = goodCollects.some(item => item.goods_id == this.data.good.goods_id)
            this.setData({
                isCollect
            })
        })

    },

    //收藏功能
    shoucang() {

    },

    //预览图片
    showImage(e) {
        let { pics } = this.data.good
        pics = pics.map(item => {
            return item.pics_big_url
        })
        wx.previewImage({
            current: e.currentTarget.dataset.url, // 当前显示图片的 http 链接
            urls: pics // 需要预览的图片 http 链接列表
        })
    },

    //加入购物车
    addShopforCar() {
        let goodCar = getStorageSync('goodCar') || []
        let index = goodCar.findIndex(item => item.goods_id == this.data.good.goods_id)
        if (index != -1) {
            goodCar[index].num++
        } else {
            this.data.good.num = 1
            this.data.good.checked = false
            goodCar.push(this.data.good)
        }
        setStorageSync('goodCar', goodCar)
        wx.showToast({
            title: '加入成功!',
            icon: 'success'
        })
    },
    //分享
    finxian() {
        this.onShareAppMessage()
    },

    //收藏
    collect() {
        let goodCollects = wx.getStorageSync("goodCollects") || [];
        let { good } = this.data
        let index = goodCollects.findIndex(item => item?.goods_id == good.goods_id)
        if (index === -1) {
            goodCollects.push(good)
            wx.setStorageSync('goodCollects', goodCollects)
            this.setData({
                isCollect: true
            })
        } else {
            goodCollects.splice(index, 1)
            wx.setStorageSync('goodCollects', goodCollects)
            this.setData({
                isCollect: false
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let pages = getCurrentPages();
        let page = pages[pages.length - 1]
        let cid = page.options.goods_id
        this.getGoodDetail(cid)

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        return {
            title: this.data.good.name,
            path: '/pages/goods_datail/index?goods_id=' + this.data.good.good_id,
        }
    }
})